import streamlit as st
import pandas as pd
import numpy as np
import time

# 地图数据
map_data = pd.DataFrame({
    'Map':['Prairie','Jungle','Valley','Rainbow','Golden','Sunset','Hidden','Echo','Dream'],
    'Difficulty':['Easy','Easy','Easy','Medium','Medium','Medium','Hard','Hard','Hard']
})

# 路线规划
def map_route(map_name, difficulty):
    """
    提供地图名称和难度级别，返回建议路线规划
    """
    st.write("以下是建议路线规划：")
    st.write("1. 前往起始位置：City of Beginnings")
    time.sleep(1)
    st.write("2. 前往地图入口：{} Entrance".format(map_name))
    time.sleep(1)
    st.write("3. 完成地图：{}".format(map_name))
    time.sleep(1)
    st.write("4. 前往下一个地图：{} Entrance".format(next_map(map_name, difficulty)))
    time.sleep(1)
    st.write("5. 完成下一个地图：{}".format(next_map(map_name, difficulty)))
    time.sleep(1)
    st.write("6. 重复步骤4-5，直到完成所有地图")

def next_map(map_name, difficulty):
    """
    根据地图名称和难度级别，返回下一个地图名称
    """
    if difficulty == 'Easy':
        if map_name == 'Prairie':
            return 'Jungle'
        elif map_name == 'Jungle':
            return 'Valley'
        elif map_name == 'Valley':
            return 'Prairie'
    elif difficulty == 'Medium':
        if map_name == 'Rainbow':
            return 'Golden'
        elif map_name == 'Golden':
            return 'Sunset'
        elif map_name == 'Sunset':
            return 'Rainbow'
    else:
        if map_name == 'Hidden':
            return 'Echo'
        elif map_name == 'Echo':
            return 'Dream'
        elif map_name == 'Dream':
            return 'Hidden'

# 自动化游戏操作
def pick_candles(strategy, steps):
    st.write("正在执行{}策略...".format(strategy))
    time.sleep(1)
    for i in range(steps):
        st.write("已完成烛火代码收集！ - 第{}次操作".format(i+1))
        time.sleep(1)


# 烛火代码管理和导出
def manage_candles(candles, export_format):
    if candles == []:
        st.write("当前没有烛火代码！")
    else:
        st.write("当前烛火代码如下：")
        for code in candles:
            st.write(code)
        if export_format == 'Excel':
            df = pd.DataFrame({'Code':candles})
            df.to_excel('candles.xlsx', index=False)
            st.write("已将烛火代码导出为Excel文件！")
        elif export_format == 'CSV':
            df = pd.DataFrame({'Code':candles})
            df.to_csv('candles.csv', index=False)
            st.write("已将烛火代码导出为CSV文件！")
        elif export_format == 'JSON':
            df = pd.DataFrame({'Code':candles})
            df.to_json('candles.json', orient='records')
            st.write("已将烛火代码导出为JSON文件！")

# 主函数
def main():
    st.title("光遇游戏跑图捡烛火代码")
    st.sidebar.title("功能选择")
    options = ['地图数据和路线规划', '自动化游戏操作', '烛火代码管理和导出']
    choice = st.sidebar.selectbox("请选择功能", options)

    if choice == '地图数据和路线规划':
        st.header("地图数据和路线规划")
        st.write(map_data)

        st.subheader("路线规划")
        map_name = st.selectbox("请选择地图", map_data['Map'])
        difficulty = map_data[map_data['Map']==map_name]['Difficulty'].values[0]
        st.write("你选择了地图：{}".format(map_name))
        map_route(map_name, difficulty)

    elif choice == '自动化游戏操作':
        st.header("自动化游戏操作")
        st.subheader("捡取策略")
        strategy = st.radio("请选择捡取策略", ['普通策略', '高效策略', '随机策略'])
        steps = st.slider("请选择自动捡取的次数", min_value=1, max_value=10, value=5, step=1)
        st.write("你选择了{}策略，自动捡取{}次".format(strategy, steps))
        pick_candles(strategy, steps)

    elif choice == '烛火代码管理和导出':
        st.header("烛火代码管理和导出")
        st.subheader("烛火代码")
        candles = st.text_input("请在下方输入烛火代码（以英文逗号隔开）")
        candles = list(candles.split(","))
        export_format = st.radio("请选择导出格式", ['Excel', 'CSV', 'JSON'])
        manage_candles(candles, export_format)

if __name__ == '__main__':
    main()